package com.dunqi.gpm.cangxi.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.base.BaseAct;
import com.dunqi.gpm.cangxi.common.view.ComTitleBar;
import com.dunqi.gpm.cangxi.home.adapter.ViewPagerFragmentAdapter;
import com.dunqi.gpm.cangxi.home.bean.Project;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.home.fragment.AuditEarlyInfoFragment;
import com.dunqi.gpm.cangxi.home.fragment.YearAmountInfoFragment;
import com.dunqi.gpm.cangxi.home.widget.TwoInOne;

import java.util.ArrayList;

import butterknife.BindView;

public class AuditYearPlanActivity extends BaseAct {

    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.two_in_one)
    TwoInOne twoInOne;

    private int year;
    private long projectId;

    String[] titles = {"年度投资信息", "前期信息"};
    ArrayList<Fragment> fragments = new ArrayList<>();
    private int ProjectAutidRequestCode = 4333;
    private Project project;
    private long proAnnualPlanId;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {
        Intent intent = getIntent();

        project = (Project) intent.getSerializableExtra(Constance.PROJECT);
        year = project.getPlanYear();
        projectId = project.getProjectId();

        proAnnualPlanId = project.getProAnnualPlanId();


        titleBar.setTitle("审核" + year + "年年度计划");

        fragments.add(YearAmountInfoFragment.newInstance(year, projectId));

        fragments.add(AuditEarlyInfoFragment.newInstance(proAnnualPlanId, projectId));
        viewPager.setAdapter(new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragments, titles));
        tabLayout.setupWithViewPager(viewPager);

        titleBar.setOnClickListenerOfRight(v -> {
            Project project = new Project();
            project.setProjectId(projectId);
            ProjectDetailActivity.start(getActivity(), project, 0);
        });


        twoInOne.setOnTwoClickListener(v -> {
            //审核通过
            audit(1);
        }, v -> {
            //审核不通过
            audit(0);
        });

    }

    private void audit(final int checked) {
        ProjectPlanYearAutidActivity.start(getActivity(), project, checked, ProjectAutidRequestCode);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_audit_year_plan;
    }

    public static void start(Context context, Project project) {
        Intent intent = new Intent(context, AuditYearPlanActivity.class);
        intent.putExtra(Constance.PROJECT, project);
        context.startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //ProjectPlanYearAutidActivity
        if (resultCode ==ProjectPlanYearAutidActivity.RESULT_CODE) {
            finish();
        }
    }
}
